//
//  TopicWriteController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/9.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "TopicWriteController.h"
#import "UIButton+BCYImage.h"

@interface TopicWriteController ()<UITextViewDelegate>
{
    UITextView *textV;
    UIButton *closeBtn;
    UILabel *placeholderL;
    
    NSString *groupId;
    
    UIButton *sendBtn;
    
}
@end

@implementation TopicWriteController

-(void)dealloc
{
    NSLog(@"TopicWriteController dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)viewDidAppear:(BOOL)animated
{
    [textV becomeFirstResponder];
}

-(instancetype)initWithGroupId:(NSString *)gid
{
    self = [super init];
    
    if (self) {
        groupId = gid;
    }
    
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加观察
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
    
    [self.largeTitle setTitle:NSLocalizedString(@"tNavTopicWrite", nil)];
    [self hiddeBack];
    [self buildView];

}

-(void)buildView
{
    
    closeBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 44, 44));
        make.addImage([UIImage imageNamed:@"nav_close_b"],UIControlStateNormal);
        make.addTarget(self,@selector(close));
        [self.largeTitle addSubview:make];
    }];
    [closeBtn setCenterByImageSize:(CGSizeMake(17, 17))];
    
    UIView *toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, 44)];
    toolbar.backgroundColor = COLOR_WHITE;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, 1)];
    line.backgroundColor = FBEE_COLOR_LINE;
    [toolbar addSubview:line];
    //自定义键盘
    {
        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        okBtn.frame = CGRectMake(IPHONEWIDTH - 70 - 10, 7, 70, 30);
        [okBtn setTitle:@"完成" forState:UIControlStateNormal];
        okBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        [okBtn addTarget:self action:@selector(keyboardHidde) forControlEvents:UIControlEventTouchUpInside];
        [toolbar addSubview:okBtn];
    }
    
    textV = [[UITextView alloc] initWithFrame:CGRectMake(25, self.largeTitle.bottom + 20, IPHONEWIDTH-50, IPHONEHEIGHT- NavBarHeight - 20)];
    textV.tintColor =FBEE_COLOR_ASSISTANT;
    [textV setFont:[UIFont systemFontOfSize:16.0f]];
    textV.delegate = self;
    textV.inputAccessoryView = toolbar;
    [self.view addSubview:textV];
    
    placeholderL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addText(@"来吧，尽情发挥吧…");
        make.addTextColor(FBEE_COLOR_INFO).addFont([UIFont systemFontOfSize:16.0f]);
        make.setFrame(CGRectMake(10, 10, 150, 17));
        [textV addSubview:make];
    }];
 
    sendBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.addFont([UIFont systemFontOfSize:13.0f]).addTitleColor(FBEE_COLOR_THEME,UIControlStateNormal);
        make.addTitle(NSLocalizedString(@"mSend", nil)).addBackGoundColor(FBEE_COLOR_ASSISTANT);
        make.addTarget(self,@selector(commitData));
        make.setFrame(CGRectMake(IPHONEWIDTH - 70 - 15, StatusHeight + (44-30)/2, 70, 30));
        make.layer.cornerRadius = 8;
        [self.largeTitle addSubview:make];
    }];
    
}

#pragma mark - TextViewDelegate

-(void)textViewDidChange:(UITextView *)textView
{
    if(textView.text.length>0)
    {
        placeholderL.hidden = YES;
    }
    else
    {
        placeholderL.hidden = NO;
    }
}


-(void)close
{
    [textV resignFirstResponder];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([KeyBoardManager sharedManager].animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    });
}


- (void)keyboardWillShow:(NSNotification *)notification {

    //改变textview高度
    textV.height = IPHONEHEIGHT - textV.top - [KeyBoardManager sharedManager].keyHeight;
    
}

- (void)keyboardDidHide:(NSNotification *)notification {
//    [UIView beginAnimations:nil context:nil];
    textV.height = IPHONEHEIGHT- NavBarHeight - 20;
//    [UIView commitAnimations];
}


-(void)commitData
{
    [textV resignFirstResponder];

    
    if (textV.text.length == 0) {
        return;
    }
    
    
    [self post:BeeGroupCreateTopic_URL param:@{@"uid":[UserInfoManager sharedManager].userId,@"gid":groupId,@"content":textV.text} code:BeeGroupCreateTopic_URL_Code];
    [LoadingHelper showLoadingOnView:self.view];
}

-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
    NSString *info = SafeGetStringValue([data objectForKey:@"msg"]);
    [NoticePopHelper showNoticeText:info];
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_WriteTopicSuccess object:nil];
    [self close];
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
    
    [LoadingHelper removeLoadingOnView:self.view];
}


-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
}


-(void)keyboardHidde
{
    [textV resignFirstResponder];
}

@end
